from typing import List
from app.parameter import MessageItem,PuttyChatParam
from langchain_core.messages import SystemMessage, AIMessage, HumanMessage

ROLEMAP = {
    "人类": AIMessage,
    "Human": HumanMessage,
    "AI": AIMessage,
    "人工智能体": AIMessage,
    "AI助手": AIMessage
}

def loadHistory(history: List[MessageItem], useReference: bool = False, offical :bool = False):
    if offical:
        content = []
        if useReference:
            for item in history:
                if item.reference != None:
                    for reference in item.reference:
                        content.append(SystemMessage(content=reference.content))    
                content.append(ROLEMAP[item.role](content = item.message))
        else:
            for item in history:
                content.append(ROLEMAP[item.role](content = item.message))
        return content
    
    content = ""
    if useReference:
        for item in history:
            if item.reference != None:
                for reference in item.reference:
                    content += "已知: " + reference.content +"\n"    
            content += item.role + ": " + item.message + "\n"
    else:
        for item in history:
            content += item.role + ": " + item.message + "\n"

    return content